package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author lixu
 */
@Service
public class DtsAdminServiceImpl implements DtsAdminService {

    @Autowired
    private DtsAdminMapper dtsAdminMapper;

    /**
     * 查询管理员的分页 及列表
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param username
     * @return
     */
    @Override
    public IPage<DtsAdmin> findPageByQuery(Integer page, Integer limit, String sort, String order, String username) {
        return dtsAdminMapper.selectPage(
                new Page<DtsAdmin>(page,limit),
                new QueryWrapper<DtsAdmin>()
                .like(!StringUtils.isEmpty(username),"username",username)
                .orderBy(!StringUtils.isEmpty(sort),"ASC".equalsIgnoreCase(order),sort));
    }

    //修改管理员

    @Override
    public void update(DtsAdmin dtsAdmin) {
        dtsAdmin.setPassword(new BCryptPasswordEncoder().encode(dtsAdmin.getPassword()));

        dtsAdmin.setUpdateTime(new Date());

        dtsAdminMapper.updateById(dtsAdmin);

    }

    /**
     * 根据用户名查询用户集合
     * @param username
     * @return
     */
    @Override
    public List<DtsAdmin> findAdmin(String username) {
        QueryWrapper<DtsAdmin> wapper = new QueryWrapper<>();
        wapper.eq("username", username);
        wapper.eq("deleted", false);
        List<DtsAdmin> list = dtsAdminMapper.selectList(wapper);
        return list;
    }

    /**
     * 根据主键id查询用户
     * @param id
     * @return
     */
    @Override
    public DtsAdmin findAdmin(Integer id) {
        return dtsAdminMapper.selectById(id);
    }

    /**
     * 分页查询
     */
    @Override
    public IPage<DtsAdmin> querySelective(String username, Integer page, Integer limit, String sort, String order) {
        IPage<DtsAdmin> ipage = new Page<>(page, limit);

        //设置查询条件
        QueryWrapper<DtsAdmin> wapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wapper.like("username", username);
        }
        wapper.eq("deleted", false);

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }

        }

        //分页查询
        IPage<DtsAdmin> pageResult = dtsAdminMapper.selectPage(ipage, wapper);

        return pageResult;
    }

    /**
     * 根据id更新用户
     * @param admin
     * @return
     */
    @Override
    public int updateById(DtsAdmin admin) {
        admin.setUpdateTime(new Date());
        return dtsAdminMapper.updateById(admin);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        dtsAdminMapper.deleteById(id);
    }

    /**
     * 添加用户
     * @param admin
     */
    @Override
    public void add(DtsAdmin admin) {
        admin.setAddTime(new Date());
        admin.setUpdateTime(new Date());
        dtsAdminMapper.insert(admin);
    }
}
